package com.fastadmin.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.fastadmin.config.quartz.QuartzJob;
import com.fastadmin.config.security.annotation.AnonymousAccess;
import com.fastadmin.mapper.impl.QuartzJobMapper;
import com.fastadmin.pojo.FastParam;
import com.xphsc.easyjdbc.page.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequiredArgsConstructor
@Slf4j

public class QuartzJobController extends BaseController {

    private final QuartzJobMapper quartzjobMapper;

    @GetMapping("sys/quartzjob/index")
    @AnonymousAccess
    public String index() {
        return "sys/quartzjob/index";
    }

    @GetMapping("sys/quartzjob/add")
    public String add() {
        return "sys/quartzjob/add";
    }

    @GetMapping("sys/quartzjob/edit")
    public String index(@RequestParam String ids, Model model) {
        QuartzJob quartzjob = this.quartzjobMapper.findById(ids);
        model.addAttribute("quartzjob", quartzjob);
        return "sys/quartzjob/edit";
    }

    @RequestMapping("sys/quartzjob/data")
    @ResponseBody
    public Object data(FastParam fastParam) {
        PageInfo<QuartzJob> pageInfo = this.quartzjobMapper.findByFastParam(fastParam);
        return datagrid(pageInfo);
    }

    @RequestMapping("sys/quartzjob/del")
    @ResponseBody
    public Object del(String ids) {
        String[] split = StrUtil.split(ids, ",");
        for (String s : split) {
            quartzjobMapper.deleteById(s);
        }
        return ok();
    }

    @RequestMapping("sys/quartzjob/save")
    @ResponseBody
    public Object save(QuartzJob quartzjob) {
        if (ObjectUtil.isNotNull(quartzjob.getId())) {
            //修改
            quartzjobMapper.update(quartzjob);
        } else {
            //保存
            quartzjobMapper.save(quartzjob);
        }
        return ok();
    }

}
